<?php
####################################################
# class for generation SiteMap 
####################################################
 
 /*
 
  <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>

 
 */
 

function get_xml_by_url($url,$changefreq='-1',$priority='0.5')
{
	
		$str_changefreq = $changefreq!='-1' ? '<changefreq>'.$changefreq.'</changefreq>' : '';
		$str_priority = "<priority>$priority</priority>";
		$str_loc = '<loc>'.$url.'</loc>';
		$xml = '<url>'.$str_loc.$str_changefreq.$str_priority.'</url>';
		
	
	return $xml;
}

 
function get_xml_by_array($arr,$changefreq='-1',$priority='0.5')
{
	$xml = '';
	
	foreach($arr as $val){
		$str_changefreq = $changefreq!='-1' ? '<changefreq>'.$changefreq.'</changefreq>' : '';
		$str_priority = "<priority>$priority</priority>";
		$str_loc = '<loc>'.$val.'</loc>';
		$xml .= '<url>'.$str_loc.$str_changefreq.$str_priority.'</url>';
	}
	return $xml;
}
 
 
 
class sitemap
{
	//var $from_charset = 'windows-1251';


	function get_xml_vacancies()
	{
		$hostname = Config::getInstance()->default->hostname;
		$url = array();
		array_push($url, 'http://'.$hostname.'/vacancies/');
	
		$db = Db::getConnection();
		$list1 = $db->fetchAll("SELECT `cities`.`name`,`cities`.`id` FROM cities, vacancies 
				where vacancies.city_id = `cities`.`id` group by  `cities`.`id` ORDER BY cities.`name`");
		
		foreach($list1 as $val){
			
			array_push($url, "http://".$hostname."/vacancies/city/id/". $val["id"]."/");
			
		}
		
		$select = VacanciesPeer::getInstance()->select();
		$list = VacanciesPeer::getInstance()->fetchAll($select);
		
		foreach($list as $val){
			array_push($url, "http://".$hostname."/vacancies/vacancy/id/". $val->getId()."/");
		}
	
		
		return get_xml_by_array($url);
	}



	function get_xml_shops()
	{
		$hostname = Config::getInstance()->default->hostname;
		
		$url = array();
		array_push($url, "http://".$hostname."/shops/");
		
		$select = ShopPeer::getInstance()->select();
		$list = ShopPeer::getInstance()->fetchAll($select);
		
		foreach($list as $val){
			array_push($url, "http://".$hostname."/shops/item/id/". $val->getId()."/");
		}
	
		return get_xml_by_array($url, 'daily', 0.8);
	}



	function get_xml_other()
	{
		$hostname = Config::getInstance()->default->hostname;
		$url = array();
		array_push($url, "http://".$hostname."/feedback/");
		array_push($url, "http://".$hostname."/promo/nutcracker/");
		array_push($url, "http://".$hostname."/search/");
		array_push($url, "http://".$hostname."/producttop/");
		return get_xml_by_array($url);
	}


	function get_xml_pages()
	{
		$hostname = Config::getInstance()->default->hostname;
		
		$url = array();
		
		$select = StaticPagePeer::getInstance()->select()->where('is_pub = 1 AND is_approved=1');
		$list = StaticPagePeer::getInstance()->fetchAll($select);
		
		foreach($list as $val){
			array_push($url, "http://".$hostname."/pages/". $val->getKey()."/");
		}
	
		
		return get_xml_by_array($url, 'daily', 0.8);
	}


	function get_xml_product()
	{
		$hostname = Config::getInstance()->default->hostname;
		
		$url = array();
		
		$select = ProductPeer::getInstance()->select()->where('is_active = 1');
		$list = ProductPeer::getInstance()->fetchAll($select);
		
		foreach($list as $val){
			array_push($url, "http://".$hostname."/product/index/id/". $val->getId()."/");
		}
	
		
		return get_xml_by_array($url, 'monthly');
	}


	
	function get_xml_category()
	{
		$hostname = Config::getInstance()->default->hostname;
		
		$url = array();
		
		$db = Db::getConnection ();
		$sql = '
		select product_category.`key` as cat_key, count(product2product_category.category_id) as cnt
		from product_category
		left JOIN  (product2product_category, product) 
		ON (product2product_category.category_id = product_category.id AND
		   product2product_category.product_id = product.id)
		WHERE product.is_active =1
		GROUP BY product_category.id
		';
		$list = $db->fetchAll ( $sql );
		
		
		foreach($list as $list_elem){
		
			if($list_elem["cnt"]>20){
				$pages = ceil($list_elem["cnt"] / 20);
			}
			
			
			for($i=1;$i<= $pages;$i++){
				if($i==1)
					array_push($url, "http://".$hostname."/catalog/index/name/".$list_elem["cat_key"]."/");
				else
					array_push($url, "http://".$hostname."/catalog/index/name/".$list_elem["cat_key"]."/page/".$i."/");
			}
		
		}
		
		return get_xml_by_array($url, 'daily', 0.8);
	}
	
	
	# получить XML news
	function get_xml_news()
	{
		$hostname = Config::getInstance()->default->hostname;
		$url = array();
		array_push($url, "http://".$hostname."/news/");
		
		$select 	 = NewsPeer::getInstance()->select()->where('is_published = 1 AND is_share!=1');
		$news_list = NewsPeer::getInstance()->fetchAll($select);
		if(count($news_list)>5){
			$pages = ceil(count($news_list) / 5);
		}
		
		for($i=2;$i<= $pages;$i++){
			array_push($url, "http://".$hostname."/news/index/page/".$i."/");
		}
		
		foreach($news_list as $val){
			array_push($url, "http://".$hostname."/news/item/id/".$val->getId()."/");
		}
		
		return get_xml_by_array($url, 'daily', 0.8);
	}
	
	# получить весь XML код
	function get_xml()
	{
		$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		$hostname = Config::getInstance()->default->hostname;
		$xml .= get_xml_by_array(array('http://'.$hostname.'/'), 'hourly', 1.0);
		$xml .= $this->get_xml_news();
		$xml .= $this->get_xml_category();
		$xml .= $this->get_xml_product();
		$xml .= $this->get_xml_other();
		$xml .= $this->get_xml_pages();
		$xml .= $this->get_xml_vacancies();
		$xml .= '</urlset>';
		
		return $xml;
	}
}
?>